package com.qingxin.park.action;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.vo.MapMarker;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;

@Controller
@RequestMapping("/parkmap")
public class ParkMapAction extends BaseAction {

	@Autowired
	private FarmParkService farmParkService;
	
	
	/**
	 * 设置坐标点
	 * @param model
	 * @param type 坐标点类型
	 * @param id 坐标点ID，可能为空
	 * @param lng
	 * @param lat
	 * @param view 是否查看模式
	 * @return
	 */
	@RequestMapping(value="/setMapMarker.do", method=RequestMethod.GET)
	public String setparkmap(ModelMap model, String type, String id, String view){
		model.put("type", type);
		model.put("id", id==null ? "" : id);
		model.put("view", view==null ? false : view);
		
		if("park".equalsIgnoreCase(type) && StringUtils.isNotEmpty(id)){
			FarmPark park = farmParkService.get(id);
			if(park != null){
				MapMarker marker = new MapMarker(park.getParkid(), type, park.getParkName(), park.getRemark(), park.getLat(), park.getLng());
				model.put("marker", marker);
			}
		}
		
		return "trace/parkmap/setMapMarker";
	}
	
	
	/**
	 * 园区分布地图
	 * @param model
	 * @param datatype
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/parkmap.do", method=RequestMethod.GET)
	public String parkmap(ModelMap model, String datatype, String leftdata, HttpServletRequest request){
//		if(SystemCfg.UI_DATATYPE_PARK.equals(datatype)){
//			model.put("parkid", leftdata);
//			FarmPark park = farmParkService.get(leftdata);
//			if(park != null){
//				MapMarker marker = new MapMarker(park.getParkid(), "park", park.getParkName(), park.getRemark(), park.getLat(), park.getLng());
//				model.put("marker", marker);
//			}
//		}
		
		return "trace/parkmap/parkmap";
	}
	
	
	
	/**
	 * 获取要在地图上显示的坐标信息
	 * @param parkid
	 * @param wlng 左边界经度
	 * @param elng 右边界经度
	 * @param nlat 上边界纬度
	 * @param slat 下边界纬度
	 * @param zoom 地图缩放级别
	 * @param size 最多加载条数，小于等于0时表示不限制
	 * @param type
	 * @param markerid
	 * @param token
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/loadMapMarkers.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object loadMapMarkers(String parkid, BigDecimal wlng, BigDecimal elng,
			BigDecimal nlat, BigDecimal slat, Integer zoom, Integer size,
			String type, String markerid, String token, HttpServletRequest request){
		Map<String, Object> rsmap = new HashMap<String, Object>();
		rsmap.put("token", token);
		
		if(size == null){
			size = 0;
		}
		//园区列表
		List<MapMarker> parkList = farmParkService.listMapMarker(wlng, elng, nlat, slat, zoom, getLoginUser(request));
		rsmap.put("list", parkList);
		return rsmap;
	}
	
	
	
	
	
	/**
	 * 获取指定坐标点的详细信息
	 * @param type 坐标点类型
	 * @param id 坐标点ID
	 * @return
	 */
	@RequestMapping(value="/getMapMarker.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getMapMarker(String type, String id){
		if("park".equalsIgnoreCase(type)){
			FarmPark park = farmParkService.get(id);
			if(park != null){
				MapMarker marker = new MapMarker(park.getParkid(), type, park.getParkName(), park.getRemark(), park.getLat(), park.getLng());
				return marker;
			}
		}
		return null;
	}
	
	
	
}
